import axios from 'axios'
import { API_BASE_URL } from '../config/api'

// 获取菜单树
export function getMenuTree(params = {}) {
  const formData = new URLSearchParams()

  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      formData.append(key, value)
    }
  })
  return axios.post(`${API_BASE_URL}/actions/menuManageUrl/getMenuListLeft`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 根据菜单ID获取菜单详情（GET，参数通过 params 传递）
export function getMenuById(menuId) {
  return axios.get(`${API_BASE_URL}/actions/menuManageUrl/getById`, {
    params: { menuId }
  })
}

// 删除菜单
export function deleteMenu(menuId) {
  const formData = new URLSearchParams()
  formData.append('menuId', menuId)
  return axios.post(`${API_BASE_URL}/actions/menuManageUrl/delete`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 修改菜单
export function updateMenu(menu) {
  return axios.post(`${API_BASE_URL}/actions/menuManageUrl/update`, menu, {
    headers: { 'Content-Type': 'application/json' }
  })
}

// 查询所有角色
export function getAllRoles() {
  const formData = new URLSearchParams()
  formData.append('pageNum', 1)
  formData.append('pageSize', 9999)
  return axios.post(`${API_BASE_URL}/actions/role/list`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 查询菜单已分配角色
export function getRolesByMenuId(menuId) {
  const formData = new URLSearchParams()
  formData.append('menuId', menuId)
  return axios.post(`${API_BASE_URL}/actions/menuManageUrl/findRoleByMenu`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 给菜单分配角色
export function allocateMenuRoles({ menuId, roleIds }) {
  const formData = new URLSearchParams()
  formData.append('menuId', menuId)
  formData.append('roleIds', roleIds)
  return axios.post(`${API_BASE_URL}/actions/menuManageUrl/allocateMenuByMenu`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 新增/编辑角色
export function saveOrUpdateRole(role) {
  return axios.post(`${API_BASE_URL}/actions/role/saveOrUpdate`, role, {
    headers: { 'Content-Type': 'application/json' }
  })
}

// 根据角色ID获取已分配菜单
export function getMenuListByRoleId(roleId) {
  const formData = new URLSearchParams()
  formData.append('roleId', roleId)
  return axios.post(`${API_BASE_URL}/actions/role/getMenuList`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 分配菜单给角色（批量）
export function allocateMenuToRole({ roleId, menuIds }) {
  const formData = new URLSearchParams()
  formData.append('roleId', roleId)
  formData.append('menuIds', menuIds)
  return axios.post(`${API_BASE_URL}/actions/menuManageUrl/allocateMenu`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 删除角色
export function deleteRole(id) {
  const formData = new URLSearchParams()
  formData.append('id', id)
  return axios.post(`${API_BASE_URL}/actions/role/delete`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}
